import { Media, MediaResolution } from '#shared/entities.js';
import * as Path from 'path';
import { CORE_STORE_CDN_BASE_URL, CORE_STORE_ROOT_DIR } from '../conf.js';
import { UserContext } from '../user-context.js';
import { symbolDic } from '../utils.js';
import { WksScopedDao } from './dao-wks-scoped.js';

const ERROR = symbolDic(
	'MEDIA_UPLOAD_FAIL_NO_WKSID',
)

export class MediaDao extends WksScopedDao<Media, number> {

	constructor() { super({ table: 'media', stamped: true }) }

	//#region    ---------- Data Entity Processing Override ---------- 
	parseRecord(dbRec: any): Media {
		dbRec.url = `${CORE_STORE_CDN_BASE_URL}${CORE_STORE_ROOT_DIR}${dbRec.folderPath}${dbRec.name ?? dbRec.srcName}`;
		if (dbRec.sd) {
			dbRec.sdUrl = `${CORE_STORE_CDN_BASE_URL}${CORE_STORE_ROOT_DIR}${dbRec.folderPath}${getResMp4Name(dbRec.name, dbRec.sd)}`;
		}
		return dbRec as Media;
	}
	//#endregion ---------- /Data Entity Processing Override ---------- 

	/** Override the baseDao.create to require 'type' and 'name' */
	async create(utx: UserContext, data: Partial<Media> & Pick<Media, 'type' | 'name'>): Promise<number> {
		return super.create(utx, data);
	}

}

export function getResMp4Name(fileName: string, res: MediaResolution) {
	return Path.parse(fileName).name + `-${res}.mp4`;
}
